package top.kaoshanji.p2ch15a;

/**
 * 代码清单-p2ch15a-444页a：死锁示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/23 18:23
 */
public class DeadLockDemo {

    private static Object lockA = new Object();
    private static Object lockB = new Object();

    private static void startThreadA() {
        Thread aThread = new Thread() {

            @Override
            public void run() {
                // 获得 lockA 之后，继续获得 lockB
                synchronized (lockA) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        /// /
                    }

                    synchronized (lockB) {}
                }
            }
        };

        aThread.start();
    }

    private static void startThreadB() {
        Thread bThread = new Thread() {

            @Override
            public void run() {

                // 获得 lockB 之后，继续获得 lockA
                synchronized (lockB) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        /// /
                    }

                    synchronized (lockA) {}
                }
            }
        };

        bThread.start();

    }

    public static void main(String[] args) {

        startThreadA();
        startThreadB();

    }


}
